package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.pasture.*;
import com.yanqu.road.entity.activity.pasture.config.PastureAnimalConfig;
import com.yanqu.road.entity.activity.pasture.config.PastureSkillConfig;
import com.yanqu.road.pb.activity.PastureProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.List;

public class PasturePb {

    /**
     * 用户数据
     */
    public static PastureProto.PastureUserDataTemp.Builder parseUserDataTemp(PastureUserData userData) {
        if (userData == null) {
            return null;
        }
        PastureProto.PastureUserDataTemp.Builder builder = PastureProto.PastureUserDataTemp.newBuilder();
        builder.setMaxScore(userData.getMaxScore());
        builder.setTodayTimes(userData.getTodayTimes());
        // 游戏数据
        PastureProto.PastureGameDataTemp.Builder gameDataTemp = parseGameDataTemp(userData.getGameData());
        if (gameDataTemp != null) {
            builder.setGameData(gameDataTemp);
        }
        // 练习模式数据
        PastureProto.PastureGameDataTemp.Builder parseGameDataTemp = parseGameDataTemp(userData.getPracticeData());
        if (parseGameDataTemp != null) {
            builder.setPracticeData(parseGameDataTemp);
        }
        // 动物数据
        for (PastureAnimalData animalData : userData.getAnimalDataMap().values()) {
            builder.addAnimalData(parseAnimalDataTemp(animalData));
        }
        builder.setUpdateTime(userData.getUpdateTime());
        builder.setResetTime(userData.getResetTime());
        builder.setIsBuySilverToken(userData.isBuySilverToken());
        builder.setIsBuyGoldToken(userData.isBuyGoldToken());
        builder.setCanRevive(userData.isCanRevive());
        builder.setLastMaxScore(userData.getLastMaxScore());
        builder.setFinishGuide(userData.isFinishGuide());
        return builder;
    }

    /**
     * 动物数据
     */
    public static PastureProto.PastureAnimalDataTemp.Builder parseAnimalDataTemp(PastureAnimalData animalData) {
        if (animalData == null) {
            return null;
        }
        PastureProto.PastureAnimalDataTemp.Builder builder = PastureProto.PastureAnimalDataTemp.newBuilder();
        builder.setAnimalId(animalData.getAnimalId());
        builder.setPromoteCnt(animalData.getPromoteCnt());
        return builder;
    }

    /**
     * 游戏数据
     */
    public static PastureProto.PastureGameDataTemp.Builder parseGameDataTemp(PastureGameData gameData) {
        if (gameData == null) {
            return null;
        }
        PastureProto.PastureGameDataTemp.Builder builder = PastureProto.PastureGameDataTemp.newBuilder();
        for (List<PastureBlockData> blockDataList : gameData.getBlockDataList()) {
            PastureProto.PastureBlockDataListTemp.Builder blockDataListBuilder = PastureProto.PastureBlockDataListTemp.newBuilder();
            for (PastureBlockData blockData : blockDataList) {
                PastureProto.PastureBlockDataTemp.Builder blockDataBuilder = parseBlockDataTemp(blockData);
                if (blockDataBuilder != null) {
                    blockDataListBuilder.addBlockData(blockDataBuilder);
                }
            }
            builder.addBlockDataList(blockDataListBuilder);
        }
        // 预览数据
        PastureProto.PastureBlockDataListTemp.Builder blockDataListBuilder = PastureProto.PastureBlockDataListTemp.newBuilder();
        // 返回首行预览数据
        if (gameData.getPreviewBlockDataList().size() > 0) {
            List<PastureBlockData> blockDataList = gameData.getPreviewBlockDataList().get(0);
            for (PastureBlockData blockData : blockDataList) {
                PastureProto.PastureBlockDataTemp.Builder blockDataBuilder = parseBlockDataTemp(blockData);
                if (blockDataBuilder != null) {
                    blockDataListBuilder.addBlockData(blockDataBuilder);
                }
            }
        }
        builder.setPreviewBlockData(blockDataListBuilder);
        // 当前积分
        builder.setScore(gameData.getScore());
        // 当前能量
        builder.setEnergy(gameData.getEnergy());
        // 游戏状态
        builder.setGameStatus(gameData.getGameStatus());
        // 回合数
        builder.setRound(gameData.getRound());
        // 冻结的回合数
        builder.setFreezeRound(gameData.getFreezeRound());
        // 本次操作消除的数量
        builder.setChainClearCnt(gameData.getChainClearCnt());
        // 本局使用充能号角的数量
        builder.setUseSkillItemCnt(gameData.getUseSkillItemCnt());
        // 本局使用了多少次技能
        builder.setUseSkillCnt(gameData.getUseSkillCnt());
        // 使用复活次数
        builder.setRestartCnt(gameData.getRestartCnt());
        // 免费复活次数
        builder.setFreeRestartCnt(gameData.getFreeRestartCnt());
        return builder;
    }

    /**
     * 格子数据
     */
    public static PastureProto.PastureBlockDataTemp.Builder parseBlockDataTemp(PastureBlockData blockData) {
        if (blockData == null) {
            return null;
        }
        PastureProto.PastureBlockDataTemp.Builder builder = PastureProto.PastureBlockDataTemp.newBuilder();
        builder.setLeft(blockData.getLeft());
        builder.setRight(blockData.getRight());
        builder.setY(blockData.getY());
        builder.setAnimalId(blockData.getAnimalId());
        builder.setScore(blockData.getScore() + blockData.getExtraScore());
        builder.setLength(blockData.getLength());
        builder.setEnergy(blockData.getEnergy() + blockData.getExtraEnergy());
        builder.setUseSkill(blockData.isUseSkill());
        builder.setDirection(blockData.getDirection());
        return builder;
    }

    /**
     * 移动数据
     */
    public static PastureProto.PastureMoveResultMsg.Builder parseMoveData(PastureGameData gameData) {
        if (gameData == null) {
            return null;
        }
        PastureProto.PastureMoveResultMsg.Builder moveMsg = PastureProto.PastureMoveResultMsg.newBuilder();
        for (PastureMoveStepResult pastureMoveStepResult : gameData.getMoveStepResultList()) {
            if (pastureMoveStepResult.getPastureMoveBlockResultList().size() == 0) {
                continue;
            }
            PastureProto.PastureStepMoveDataTemp.Builder stepData = PastureProto.PastureStepMoveDataTemp.newBuilder();
            for (PastureMoveBlockResult moveResult : pastureMoveStepResult.getPastureMoveBlockResultList()) {
                PastureProto.PastureBlockMoveDataTemp.Builder blockDataTemp = PastureProto.PastureBlockMoveDataTemp.newBuilder();
                PastureProto.PastureBlockDataTemp.Builder originBlockData = PasturePb.parseBlockDataTemp(moveResult.getOriginBlockData());
                if (originBlockData != null) {
                    blockDataTemp.setOriginBlockData(originBlockData);
                }
                PastureProto.PastureBlockDataTemp.Builder targetBlockData = PasturePb.parseBlockDataTemp(moveResult.getTargetBlockData());
                if (targetBlockData != null) {
                    blockDataTemp.setTargetBlockData(targetBlockData);
                }
                // 移动类型
                blockDataTemp.setMoveType(moveResult.getMoveType());
                stepData.addBlockMoveData(blockDataTemp);
            }
            stepData.setScore(pastureMoveStepResult.getScore());
            stepData.setEnergy(pastureMoveStepResult.getEnergy());
            moveMsg.addStepMoveData(stepData);
        }
        return moveMsg;
    }

    /**
     * 同步配置
     */
    public static PastureProto.SyncPastureConfigMsg.Builder parseSyncConfigTemp(PastureConfig config, String language) {
        if (config == null) {
            return null;
        }
        PastureProto.SyncPastureConfigMsg.Builder builder = PastureProto.SyncPastureConfigMsg.newBuilder();
        // 动物配置
        config.getAnimalConfigMap().values().forEach(animalConfig -> {
            PastureProto.PastureAnimalTemp.Builder animalConfigBuilder = parseAnimalConfigTemp(animalConfig);
            if (animalConfigBuilder != null) {
                builder.addAnimalConfig(animalConfigBuilder);
            }
        });
        // 技能配置
        config.getSkillConfigMap().values().forEach(skillConfig -> {
            PastureProto.PastureSkillTemp.Builder skillConfigBuilder = parseSkillConfigTemp(skillConfig, language);
            if (skillConfigBuilder != null) {
                builder.addSkillConfig(skillConfigBuilder);
            }
        });
        return builder;
    }

    /**
     * 动物配置
     */
    public static PastureProto.PastureAnimalTemp.Builder parseAnimalConfigTemp(PastureAnimalConfig animalConfig) {
        if (animalConfig == null) {
            return null;
        }
        PastureProto.PastureAnimalTemp.Builder builder = PastureProto.PastureAnimalTemp.newBuilder();
        builder.setAnimalId(animalConfig.getAnimalId());
        builder.setSkillId(animalConfig.getSkillId());
        builder.setSize(animalConfig.getSize());
        builder.addAllPoint(animalConfig.getPoint());
        builder.addAllCost(animalConfig.getCost());
        builder.setEnergy(animalConfig.getEnergy());
        builder.setDisplay(animalConfig.getDisplay());
        builder.setBurst(animalConfig.getBurst());
        return builder;
    }

    /**
     * 技能配置
     */
    public static PastureProto.PastureSkillTemp.Builder parseSkillConfigTemp(PastureSkillConfig skillConfig, String language) {
        if (skillConfig == null) {
            return null;
        }
        PastureProto.PastureSkillTemp.Builder builder = PastureProto.PastureSkillTemp.newBuilder();
        builder.setSkillId(skillConfig.getSkillId());
        builder.setSkillParam(skillConfig.getSkillParam());
        builder.setRepeat(skillConfig.getRepeat());
        builder.setDesc(ServerLanguageMgr.getContent(skillConfig.getDesc(), language));
        return builder;
    }

}
